package com.htjc.integral.exchange.controller;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.htjc.integral.exchange.service.JdgbExchangeIntegralService;
import com.htjc.integral.exchange.service.impl.JdgbExchangeIntegralServiceImpl;

import lombok.extern.slf4j.Slf4j;

@Slf4j(topic = "exchangeLogger")
@RestController
@RequestMapping(value = "/vip/service")
public class JDGBExchangeIntegralController {
	@Autowired
	private JdgbExchangeIntegralService jdgbExchangeIntegralService;

	@Autowired
	private JdgbExchangeIntegralServiceImpl jdgbExchangeIntegralServiceImpl;
	
	/**
	 * 用户授权登录接口(不用改)
	 */
	@RequestMapping(value = "/userAuthorizeLogin", method = { RequestMethod.POST, RequestMethod.GET })
	public void userAuthorizeLogin(String clientId, String timestamp, String uid, String sign,
			HttpServletResponse response) throws Exception {
		long startTime = System.currentTimeMillis();
		log.info("进入获取用户信息接口==================================时间:{}", startTime);
		response.setContentType("text/html;charset=utf-8");
		try {
			if (uid != null && !uid.equals("")) {
				String result = jdgbExchangeIntegralService.userAuthorizeLogin(clientId, timestamp, uid, sign);
				response.getWriter().print(result);
				log.info("uid不为空，返回校验信息===================================={}",result);
			} else {
				log.info("没有uid，跳转到登录页面=======登录地址：https://efree.esgmallt.com/tjsc/index.html#/authorize");

				String url = jdgbExchangeIntegralService.queryParamByCode("integral.exchange.gangbeng.authorize.path");
				response.sendRedirect(url);
			}
			
			log.info("================查询积分接口成功=====================方法总耗时:{}", System.currentTimeMillis() - startTime);

		} catch (Exception e) {
			// TODO: handle exception
			log.error("用户授权登录接口出现异常！====={}", e);
		}
	}

	/**
	 * 积分查询接口
	 */
	@RequestMapping(value = "/queryIntegral", method = { RequestMethod.POST, RequestMethod.GET })
	public void queryIntegral(String uid, String clientId, String timestamp, String description, String sign,
			HttpServletResponse response) {
		long startTime = System.currentTimeMillis();
		log.info("进入查询积分接口=====================================时间:{}", startTime);
		response.setContentType("text/html;charset=utf-8");
		try {
			String result = jdgbExchangeIntegralService.queryIntegral(uid, clientId, timestamp, sign, description);
			response.getWriter().print(result);
			log.info("================查询积分接口成功=====================返回的结果是:{}", result);
			log.info("================查询积分接口成功=====================方法总耗时:{}", System.currentTimeMillis() - startTime);
		} catch (Exception e) {
			log.error("查询积分出现异常！====={}", e);
		}

	}

	/**
	 * 积分扣减接口
	 */
	@RequestMapping(value = "/deductionIntegral", method = { RequestMethod.POST, RequestMethod.GET })
	public void deductionIntegral(String uid, String credits, String clientId, String timestamp, String description,
			String rtmOrderNum, String pointType, String sign, HttpServletResponse response) {
		long startTime = System.currentTimeMillis();
		log.info("进入扣减积分接口=====================================时间:{}", startTime);
		response.setContentType("text/html;charset=utf-8");
		String result;
		try {
			String NEW_OLD_JF_FLAG = jdgbExchangeIntegralServiceImpl.queryParamByCode("NEW_OLD_JF_FLAG");
			// 00:调用老系统
			if ("00".equals(NEW_OLD_JF_FLAG)) {
				result = jdgbExchangeIntegralService.deductionIntegral(uid, credits, clientId, timestamp,
						description, rtmOrderNum, pointType, sign);
				response.getWriter().print(result);
				log.info("================京东调用扣减积分接口成功=====================返回的结果是:{}", result);
			// 01:调用新系统
			} else if ("01".equals(NEW_OLD_JF_FLAG)) {
				result = jdgbExchangeIntegralService.deductionIntegralNew(uid, credits, clientId, timestamp, description, rtmOrderNum, pointType, sign);
				response.getWriter().print(result);
				log.info("================京东调用扣减积分接口成功=====================返回的结果是:{}", result);
			}
			log.info("================京东调用扣减积分接口成功=====================方法总耗时:{}",
					System.currentTimeMillis() - startTime);
		} catch (Exception e) {
			log.error("扣减积分出现异常！异常信息:{}", e);
			e.printStackTrace();
		}
	}

	/**
	 * 积分退回接口
	 */
	@RequestMapping(value = "/backIntegral", method = { RequestMethod.POST, RequestMethod.GET })
	public void backIntegral(String uid, String credits, String clientId, String timestamp, String description,
			String rtmOrderNum, String pointType, String sign, HttpServletResponse response) {
		long startTime = System.currentTimeMillis();
		log.info("进入退回积分接口=====================================时间:{}", startTime);
		response.setContentType("text/html;charset=utf-8");
		String result;
		try {
			String NEW_OLD_JF_FLAG = jdgbExchangeIntegralServiceImpl.queryParamByCode("NEW_OLD_JF_FLAG");
			// 00:调用老系统
			if ("00".equals(NEW_OLD_JF_FLAG)) {
				result = jdgbExchangeIntegralService.backIntegral(uid, credits, clientId, timestamp, description,
						rtmOrderNum, pointType, sign);
				response.getWriter().print(result);
				log.info("================京东调用退回积分接口成功=====================返回的结果是:{}", result);
			// 01:调用新系统
			}else if ("01".equals(NEW_OLD_JF_FLAG)) {
				result = jdgbExchangeIntegralService.backIntegralNew(uid, credits, clientId, timestamp, description, rtmOrderNum, pointType, sign);
				response.getWriter().print(result);
				log.info("================京东调用退回积分接口成功=====================返回的结果是:{}", result);
			}
			log.info("================京东调用退回积分接口成功=====================方法总耗时:{}",
					System.currentTimeMillis() - startTime);
		} catch (Exception e) {
			log.error("退回积分出现异常！异常信息:{}", e);
		}
	}

	/**
	 * 增加积分接口(无用)
	 */
	@RequestMapping(value = "/addIntegral", method = { RequestMethod.POST, RequestMethod.GET })
	public void addIntegral(String uid, String credits, String clientId, String timestamp, String description,
			String rtmOrderNum, String pointType, String sign, HttpServletResponse response) {
		log.info("进入增加积分接口=====================================");
		response.setContentType("text/html;charset=utf-8");
		try {
			response.getWriter().print(jdgbExchangeIntegralService.addIntegral(uid, credits, clientId, timestamp,
					description, rtmOrderNum, pointType, sign));
			log.info("================京东调用增加积分接口成功=====================");
		} catch (Exception e) {
			log.error("增加积分出现异常！异常信息:{}", e);
		}
	}

	/**
	 * 订单状态查询接口
	 */
	@RequestMapping(value = "/queryOrderStatus", method = { RequestMethod.POST, RequestMethod.GET })
	public void queryOrderStatus(String clientId, String timestamp, String rtmOrderNum, String sign,
			HttpServletResponse response) {
		long startTime = System.currentTimeMillis();
		log.info("进入订单状态查询接口==================================时间:{}", startTime);
		response.setContentType("text/html;charset=utf-8");
		try {
			String result = jdgbExchangeIntegralService.queryOrderStatus(clientId, timestamp, rtmOrderNum, sign);
			response.getWriter().print(result);
			log.info("================京东调用订单状态查询接口成功=====================返回的结果是:{}", result);
			log.info("================京东调用订单状态查询接口成功=====================方法总耗时:{}",
					System.currentTimeMillis() - startTime);
		} catch (Exception e) {
			log.error("查询积分出现异常！异常信息:{}", e);
		}
	}

}
